package business.cdss;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import com.google.common.base.Objects;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.HashMap;

import static common.Login.login;

public class ShowWeightTemplate {

    Logger log = Logger.getLogger(ShowWeightTemplate.class);
    public static final String system = "env.properties";
    Data data = login();

    public String showWeightTemplate() throws ConfigurationException, FileNotFoundException, YamlException {

        int status;
        JSONObject jb2=new JSONObject();
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("userId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("org_ids", "true");
        json.put("ptType", yaml.getDatas().get(0).get("ptType"));
        json.put("default", yaml.getDatas().get(0).get("default"));
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, yaml.getMethod(), yaml.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        JSONObject obj = JSON.parseObject(jsonStr);
        JSONArray jsonArray=obj.getJSONArray("resultData");

        //循环获取children下的值，再转成需要的格式
        for(Object object:jsonArray){
            JSONObject jsonObject1 = (JSONObject) object;
            JSONArray children = jsonObject1.getJSONArray("children");
            for(Object ob:children){
                JSONObject jsonObject2 = (JSONObject) ob;
                String name=jsonObject2.getString("name");
                int num=jsonObject2.getIntValue("num");
                String type=jsonObject2.getString("type");
                String value=jsonObject2.getString("value");
                //判断叶子最低端value的值，false时status设为0，num为0；true时status为1，num从object上获取
                if(Objects.equal(value,"true")){
                    status=1;
                }
                else {
                    status=0;
                    num=0;
                }
                //组json格式
                JSONObject jb=new JSONObject();
                jb.put("num",num);
                jb.put("status",status);
                jb2.put(name,jb);
            }

        }
        String jsonstr= jb2.toString();

        return jsonstr;
    }
}
